//
//  ViewController.swift
//  AddCarCore
//
//  Created by seongbrave on 03/17/2017.
//  Copyright (c) 2017 seongbrave. All rights reserved.
//

import UIKit
import RxSwift
import ModelProtocol
import UtilCore
import NetWorkCore
import Alamofire
import SwiftyUserDefaults
import AddCarCore
import BrandonCore
/// miker md5 加密
extension String {
    func getMd5Password() -> String {
        
        return "\(self)eme909014123561236jkwsdfjdw".md5()
    }
}
public enum HomePageApi{
    ///获取地址
    case firstPage
    ///获取七牛云uptoken
    case uptoken
    case getrecommendmore(page:Int,length:Int,id:String)
    /// 获取商品详情model 带sku信息的
    case glproductskus(pdid:String)
    ///显示首页搜索页面
    case seachIndex
    /*
     首页搜索界面
     
     keyword:
     required
     string美白面膜
     搜索关键字
     brid:	string
     品牌ID
     ptid:	string
     分类ID
     firistflag:
     required
     string1
     分类ID 第一次查询标志（1-是2-否）（第一次查询会返回品牌，类目，属性信息）
     pavstr:	string
     属性值，以，分隔
     oflag:	string
     排序标志（1-销量2-毛利3-价钱升序4-价钱降序） 非必填
     minamount:	bigdecimal
     金额区间开始 非必填
     maxamount:	bigdecimal
     金额区间结束
     moq:	int
     起批量
     page:
     required
     int
     起批量
     length:
     required
     int
     起批量
     */
    case seachproducts(keyword:String,brid:String,ptid:String,firistflag:String,pavstr:String,oflag:String,minamount:String,maxamount:String,moq:String,page:Int,length:Int)
    //首页 默认关键字
    case defaultkeyword
    
    case login
    
}
extension HomePageApi: TargetType {
    
    //设置请求路径
    public var path: String {
        switch self {
        case .firstPage:
            return "/firstPage/a/index.json"
        case .login:
            return "/login.json"
        case .uptoken:
            return "/uptoken.json"
        case .getrecommendmore:
            return "/firstPage/a/getrecommendmore.json"
        case .glproductskus:
            return "/glProduct/skus.json"
        case .seachIndex:
            return "/seProduct/a/seachIndex.json"
        case .seachproducts:
            return "/seProduct/a/seachproducts.json"
        case .defaultkeyword:
            return "/firstPage/a/defaultkeyword.json"
        }
    }
    
    //设置请求方式 get post等
    public var method: HTTPMethod {
        switch self {
        default :
            return .post
            
        }
    }
    /// 设置请求参数
    public var parameters: Parameters? {
        switch self {
        case  .login:
            return ["username": "15235106500", "password": "1234wert".getMd5Password()]
        case let .getrecommendmore(page, length,id):
            return ["page": page, "length": length,"id":id]
        case let .glproductskus(pdid):
            return ["pdid":pdid]
        case let .seachproducts(keyword,brid,ptid,firistflag,pavstr,oflag,minamount,maxamount,moq,page,length):
            return ["keyword":keyword,"brid":brid,"ptid":ptid,"firistflag":firistflag,"pavstr":pavstr,"oflag":oflag,"minamount":minamount,"maxamount":maxamount,"moq":moq,"page": page, "length": length]
        default :
            return nil
        }
    }
    
}


class ViewController: Base_Vc, AddCarProtocol  {

    
    @IBOutlet weak var productId_Tf: UITextField!
    @IBOutlet weak var addCart_Btn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.addCart_Btn.rx.tap.map{ HomePageApi.glproductskus(pdid: self.productId_Tf.text!)  }.emeRequestApiForObj(ProductModel.self)
            .subscribe(onNext: { [unowned self] ( result ) in
                switch result {
                case .success(let data):
                    self.addCar(vc: self, model: data, footerButonType: ""){ item in
                        
                       
                    }
                    
                case .failure(let error):
                    print(error)
                    self.toLogin()
                    
                    
                }
            }).disposed(by: disposeBag)
        
//        self.addCart_Btn.rx.tap.subscribe(onNext: {  ( _ ) in
//       
//        }).disposed(by: disposeBag)
        // Do any additional setup after loading the view, typically from a nib.
    }
    func toLogin() -> Void {
        // 只要登陆就需要重新生成devid
        Observable.just().map{_ in
            
            return HomePageApi.login
            }.emeRequestApiForRegJson()
            .subscribe(onNext: { (result) in
                switch result {
                case .success(let data):
                    print(data)
                    AppData.loginedUser =  UserModel(json: data)
                case .failure(let error):
                    
                    print(error)
                }
            }).addDisposableTo(disposeBag)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

